package com.ls.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tedu.crm.entity.Department;
import cn.tedu.crm.service.IDepartService;
import cn.tedu.crm.util.JsonResult;

@Controller
@RequestMapping("/dp")
public class DepartController {
	@Resource(name = "departService")
	private IDepartService service;

	@RequestMapping("/listdp.do")
	@ResponseBody
	public JsonResult<List<Department>> listDepart(@RequestParam(value = "pageId", defaultValue = "1") int pageId,
			@RequestParam(value = "num", defaultValue = "4") int num) {
		//System.out.println(pageId);
		JsonResult<List<Department>> result = service.selectAll(pageId, num);
		return result;
	}

	@RequestMapping("/adddp.do")
	@ResponseBody
	public JsonResult<Object> addDepart(String dept_name, String create_time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date time;
		try {
			time = format.parse(create_time);
			JsonResult<Object> result = service.insertDept(dept_name, new java.sql.Date(time.getTime()));
			return result;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;

	}

	@RequestMapping("/selectdpname.do")
	@ResponseBody
	public JsonResult<List<Department>> selectDept_name() {
		JsonResult<List<Department>> result = service.selectAllDepartment();
		return result;

	}

	@RequestMapping("/deleteid.do")
	@ResponseBody
	public JsonResult<Object> deleteDept(String[] dept_id) {
		JsonResult<Object> result = service.deleteByIdDept(dept_id);
		return result;

	}

	@RequestMapping("/selectename.do")
	@ResponseBody
	public JsonResult<List<Department>> selectName(String name,@RequestParam(value = "pageId", defaultValue = "1") int pageId,
			@RequestParam(value = "num", defaultValue = "4") int num) {
		JsonResult<List<Department>> result = service.findName(name, pageId, num);
		return result;

	}
	
	@RequestMapping("/show.do")
	@ResponseBody
	public JsonResult<Department> showDp(int dept_id) {
		JsonResult<Department> result = service.showDp(dept_id);
		return result;
		
	}
	
	@RequestMapping("/update.do")
	@ResponseBody
	public JsonResult<Object> updateCustomer(Department department) {
		JsonResult<Object> result = service.updateDp(department);
		return result;
		
	}
}
